﻿package {

	import flash.display.*;
	import flash.net.*;
	import flash.events.*;
	import flash.utils.ByteArray;
    import flash.text.*;
	import fl.controls.*;

	//import com.klstudio.images.JPGEncoder;

	public class SaveTest extends Sprite {

		private var _sp:Sprite;
		private var _savebutton:Button;
		private var _loader:Loader = new Loader();
		private var _bitmap:Bitmap;
		private var file:FileReference;
		private var inputTxt:TextField=new TextField();
        private var txtBox:TextField=new TextField();
		public function SaveTest() {
			_sp = new Sprite();
			_savebutton = new Button();
			addChild(_sp);
			addChild(_savebutton);
			_savebutton.label = "保存";
			_savebutton.move(165,179);
			_savebutton.addEventListener(MouseEvent.CLICK,savejpg);
			
			LoadImage();
			inputText();
			

		}
		private function LoadImage() {
			var url:String = "image/1.jpg";
			var urlReq:URLRequest = new URLRequest(url);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,Loaded);
			_loader.load(urlReq);
			_sp.addChild(_loader);
            _sp.addChild(txtBox);
			var _bitmap:Bitmap = new Bitmap();

		}
		private function Loaded(e:Event) {
			_bitmap = new Bitmap();
			_bitmap=(e.target.content);
		}
		public function getBitmapData():BitmapData {
			txtBox.text=inputText();
			var _bitData:BitmapData = new BitmapData(_sp.width,_sp.height);
			_bitData.draw(_sp);
			return _bitData;
		}
		private function savejpg(evt:Event):void {
			
			_savebutton.enabled = false;
			var encoder:JPGEncoder = new JPGEncoder(80);
			var bytes:ByteArray = encoder.encode(getBitmapData());
			var request:URLRequest = new URLRequest();
			request.data = bytes;
			trace(bytes);
			file = new FileReference();
			file.save(bytes,"1.jpg");
			file.addEventListener(Event.OPEN,completeSave)

			
		}
		private function completeSave(e:Event){
			_savebutton.enabled = true;
			}
		private function inputText():String {
			inputTxt.background=true;
			inputTxt.backgroundColor=0xC0C0C0;//文本背景色
			inputTxt.width=100;
			inputTxt.height=20;
			inputTxt.x=50;
			inputTxt.y=180;
			inputTxt.type=TextFieldType.INPUT;//文本类型为输入文本
			addChild(inputTxt);
			return inputTxt.text;
		}
		
		
	}
}